#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <errno.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);



//    modbusServer = modbus_new_tcp("127.0.0.1",567);//由于是tcp client 连接，在同一程序中相同的端口可以连接多次

//            modbusMaster =modbus_new_rtu("/dev/ttyUSB0",
//                                         9600, 'E',
//                                                8, 1);

//    modbus_set_slave(modbusServer,3);//从机地址
//    modbus_connect(modbusServer);
//    if (modbus_connect(modbusServer) == -1) {
//        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
//        modbus_free(modbusServer);
//    }
//    else
//    {
//        modbus_set_response_timeout(modbusServer, 0, 500000);
//        qDebug()<<"connect success";
//    }





}

MainWindow::~MainWindow()
{
    modbus_close(modbusServer);
    modbus_free(modbusServer);
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //write coil
//    modbusServer->setCoilValue(0,true);
//    modbusServer->setCoilValue(1,false);
//    modbusServer->setCoilValue(2,true);
//    modbusServer->setCoilValue(3,false);


    //    uint16_t tab_reg[32]={0};
    //    int regs=modbus_read_registers(modbusServer,0,20,tab_reg);

    //    regs=modbus_write_register(modbusServer,0x00,94);

    //    printf("%d %d %d %d %d %d %d %d\n",regs,tab_reg[0],tab_reg[1],tab_reg[2],tab_reg[3],tab_reg[4],tab_reg[5],tab_reg[6],tab_reg[7]);


}

void MainWindow::on_pushButton_2_clicked()
{
    //read


}

void MainWindow::on_pushButton_3_clicked()
{
    //get coil result

    //    uint16_t tab_reg[32]={0};
    //    int regs=modbus_read_registers(modbusServer,0,20,tab_reg);

    //    regs=modbus_write_register(modbusServer,0x00,94);

    //    printf("%d %d %d %d %d %d %d %d\n",regs,tab_reg[0],tab_reg[1],tab_reg[2],tab_reg[3],tab_reg[4],tab_reg[5],tab_reg[6],tab_reg[7]);




}

void MainWindow::on_pushButton_4_clicked()
{
    //connect port

//    modbusServer = modbus_new_tcp("127.0.0.1",567);//由于是tcp client 连接，在同一程序中相同的端口可以连接多次
//    modbusServer =modbus_new_rtu("/dev/ttyUSB0",
//                                 9600, 'E',
//                                        8, 1);
    modbusServer =modbus_new_rtu("/dev/ttyUSB0",
                                 9600, 'E',
                                        8, 1);
    modbus_set_slave(modbusServer,2);//从机地址

    if (modbus_connect(modbusServer) == -1) {
//        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
//        printf("Connection failed: %s\n");
        qDebug()<<"connect failed";
        modbus_free(modbusServer);
    }
    else
    {
        qDebug()<<"connect success";
    }
}

void MainWindow::on_pushButton_5_clicked()
{
//        //disconnect
//    modbusServer = modbus_new_tcp("127.0.0.1",567);//由于是tcp client 连接，在同一程序中相同的端口可以连接多次


//    modbus_set_slave(modbusServer,1);//从机地址

    modbus_close(modbusServer);
    modbus_free(modbusServer);
    qDebug()<<"disconnect success";
}

void MainWindow::on_pushButton_6_clicked()
{
//    modbusServer->getConnectState();
}

void MainWindow::on_pushButton_7_clicked()
{
    //write register

//    modbusServer->setHoldingRegisters(0,ui->lineEdit->text().toInt());

}

void MainWindow::on_pushButton_8_clicked()
{
    //read register
//    quint16 returnValue;
//    modbusServer->getHoldingRegisters(0,returnValue);
//    ui->lineEdit_2->setText(QString::number(returnValue));
//    qDebug()<<"getHoldingRegisters"<<returnValue;

}

void MainWindow::on_pushButton_9_clicked()
{
    //read result

}

void MainWindow::on_pushButton_10_clicked()
{
    //write hold reg

        int regs=modbus_write_register(modbusServer,ui->lineEdit_3->text().toInt(),ui->lineEdit_5->text().toInt());
        qDebug()<<"write result"<<regs;
        if(-1==regs)
        {
            fprintf(stderr, "failed: %s\n", modbus_strerror(errno));
        }




}

void MainWindow::on_pushButton_11_clicked()
{
    //read hold reg,if not connected, can not read and write, others will cause coredump

        uint16_t tab_reg[32]={0};
        int startAddress=ui->lineEdit_3->text().toInt();
        int count=ui->lineEdit_4->text().toInt();
        int regs=modbus_read_registers(modbusServer,startAddress,count,tab_reg);

        ui->lineEdit_6->setText(QString::number(tab_reg[0]));

        qDebug()<<"read result"<<regs;
        if(-1==regs)
        {
            fprintf(stderr, "failed: %s\n", modbus_strerror(errno));
        }


}

void MainWindow::on_pushButton_12_clicked()
{
    //write  coil bit


    int regs=modbus_write_bit(modbusServer,ui->lineEdit_3->text().toInt(),ui->lineEdit_5->text().toInt());
    qDebug()<<"write result"<<regs;
}

void MainWindow::on_pushButton_13_clicked()
{
    //read coil bit
    uint8_t tab_reg[32]={0};
    int startAddress=ui->lineEdit_3->text().toInt();
    int count=ui->lineEdit_4->text().toInt();

    int regs=modbus_read_bits(modbusServer,startAddress,count,tab_reg);

    ui->lineEdit_6->setText(QString::number(tab_reg[0]));

    qDebug()<<"read result"<<regs;
}

void MainWindow::on_pushButton_14_clicked()
{
    //write input bit


    //int regs=modbus_write_bit(modbusServer,ui->lineEdit_3->text().toInt(),ui->lineEdit_5->text().toInt());
    qDebug()<<"no runction ";
}

void MainWindow::on_pushButton_15_clicked()
{
    //read input bit
    uint8_t tab_reg[32]={0};
    int startAddress=ui->lineEdit_3->text().toInt();
    int count=ui->lineEdit_4->text().toInt();

    int regs=modbus_read_input_bits(modbusServer,startAddress,count,tab_reg);

    ui->lineEdit_6->setText(QString::number(tab_reg[0]));

    qDebug()<<"read result"<<regs;
}
